package com.share.demo.controller;

import com.share.demo.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * author:caifan
 * date:2019/6/27
 */
@RestController
@RequestMapping("/rest/excel")
@Slf4j
public class ExcelController {

    @RequestMapping(value = "/exportExcel", method = RequestMethod.POST)
    public void exportExcel(HttpServletResponse response, @RequestBody Map<String, Object> map) throws Exception {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("漏话回访");
        //创建表头
        setTitle(workbook, sheet);
        //新增数据行，并且设置单元格数据
        Row row = sheet.createRow(1);
        Calendar c = Calendar.getInstance();
        c.set(2010, 7, 1);
        row.createCell(0, CellType.STRING).setCellValue("张三");
        row.createCell(1, CellType.NUMERIC).setCellValue(30);
        /**
         * date类型
         */
        Cell cell = row.createCell(2, CellType.NUMERIC);
        cell.setCellValue(c.getTime());
        CreationHelper creationHelper = workbook.getCreationHelper();
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("yyyy-MM-dd HH:mm:ss"));
        cell.setCellStyle(cellStyle);
        row.createCell(3, CellType.NUMERIC).setCellValue(10000.22d);
        // SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        String fileName = "Template-" + new Date().getTime() + ".xlsx";
        //清空response
        response.reset();
        //设置response的Header
        response.addHeader("Content-Disposition", "attachment;filename="+ fileName);
        OutputStream os = new BufferedOutputStream(response.getOutputStream());
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        //将excel写入到输出流中
        workbook.write(os);
        log.info("name:{}", map.get("name"));
        os.flush();
        os.close();
    }

    // 创建表头
    private void setTitle(Workbook workbook, Sheet sheet) {
        Row row = sheet.createRow(0);
        // 设置列宽，setColumnWidth的第二个参数要乘以256，这个参数的单位是1/256个字符宽度
        sheet.setColumnWidth(2, 60 * 256);
        // 设置为居中加粗
        CellStyle style = workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        Font font = workbook.createFont();
        font.setBold(true);
        style.setFont(font);
        //导出的Excel头部
        String[] headers = {"姓名", "年龄", "大学毕业时间", "月薪"};
        // 设置表格默认列宽度为15个字节
        sheet.setDefaultColumnWidth((short) 16);
        for (short i = 0; i < headers.length; i++) {
            Cell cell = row.createCell(i);
            RichTextString text = new XSSFRichTextString(headers[i]);
            cell.setCellValue(text);
            cell.setCellStyle(style);
        }
    }

    @RequestMapping(value = "/getMapParam", method = RequestMethod.GET)
    public String paramMap(@RequestParam String type, @RequestBody Map<String, Object> map) {
        log.info("map:{}", map.toString());
        log.info("name:{}", map.get("name"));
        log.info("type:{}", type);
        return "OK";

    }
}
